﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Code.Models;
using Code.BLL;
using Code.Commen;
using Company.Web.Models.ProductsModels;

namespace Company.Web.Controllers
{
    public class ProductsController : ControllerBase
    {
        ProductManage PM = new ProductManage();

        public ActionResult Index(IndexModel model)
        {
            if (model == null)
                model = new IndexModel();
            if (model.Serch == null)
                model.Serch = new IndexSerchModel();
            if (model.B < 1)
                model.B = null;
            if (model.C < 1)
                model.C = null;

            var page = model.B ?? 1;
            var size = model.C ?? 12;

            model.Parents = new List<TypeItem>();
            model.Childs = new List<TypeItem>();
            if (model.A.HasValue)
            {
                var type = CommenManage.ProductTypes.FirstOrDefault(p => p.ID == model.A);
                if (type != null)
                {
                    var parents = type.Parents.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToArray();
                    model.Parents.AddRange(CommenManage.ProductTypes.Where(p => parents.Contains(p.ID)).OrderBy(p => p.Parents.Length)
                        .Select(p => new TypeItem { ID = p.ID, Name_Ch = p.Name_Ch, Name_En = p.Name_En, ParentID = p.ParentID, Parents = p.Parents }));
                    model.Parents.Add(new TypeItem { ID = type.ID, Name_Ch = type.Name_Ch, Name_En = type.Name_En, ParentID = type.ParentID, Parents = type.Parents });
                }
            }
            var pcount = PM.GetTable<Product>().Where(p => p.ProductType.Parents.Contains(model.A + ",")).Select(p => new { p.ProductType.ID, p.ProductType.Parents }).ToArray();
            var childs = CommenManage.ProductTypes.Where(p => p.ParentID == model.A || (p.ParentID == null && model.A == null));
            model.Childs.AddRange(childs.Select(p => new TypeItem
            {
                ID = p.ID,
                Name_Ch = p.Name_Ch,
                Name_En = p.Name_En,
                ParentID = p.ParentID,
                Parents = p.Parents,
                ProductCount = pcount.Where(p1 => p1.ID == p.ID || p1.Parents.Contains(p.ID + ",")).Count()
            }));

            //model.Types = CommenManage.ProductTypes.ToArray();

            var products = PM.GetTable<Product>();
            products = products.Where(p => p.TypeID == model.A || (model.A == null && p.ProductType.ParentID == null) || p.ProductType.Parents.Contains("," + (model.A ?? 0) + ","));
            if (!string.IsNullOrEmpty(model.Serch.Number))
                products = products.Where(p => p.Identifier.Contains(model.Serch.Number) || p.ProductInfo_Ch.Name.Contains(model.Serch.Number) || p.ProductInfo_En.Name.Contains(model.Serch.Number));



            //var defpic = CommenManage.ProductPictureTypes.FirstOrDefault(p => p.IsDefault);
            //int def = defpic == null ? -1 : defpic.ID;
            model.SumCount = products.Count();
            model.Products = products.OrderBy(p => p.ID).Skip(size * (page - 1)).Take(size)
                .Select(p => new ProductItem
                {
                    ID = p.ID,
                    Number = p.Identifier,
                    Name_Ch = p.ProductInfo_Ch.Name,
                    Name_En = p.ProductInfo_En.Name,
                    DefaultPic = p.ProductPictures.OrderBy(pp => pp.IsDefault).Select(pp => pp.FileName).FirstOrDefault(),
                    TypeID = p.TypeID,
                    TypePath = p.ProductType.Parents
                })
                .ToArray().Each(p =>
                {
                    var tmp = CommenManage.ProductTypes.Where(p1 => p1.ID == p.TypeID || p.TypePath.Contains("," + p1.ID + ",")).OrderBy(p1 => p1.Parents.Length);
                    p.TypePath = string.Join("->", tmp.Select(p1 => model.l == Language.Ch ? p1.Name_Ch : p1.Name_En).ToArray());
                }).ToArray();

            return View("Index_" + model.l, model);
        }


        public ActionResult Detail(long A,Language L)
        {
            var defpic = CommenManage.ProductPictureTypes.FirstOrDefault(p => p.IsDefault);
            int def = defpic == null ? -1 : defpic.ID;
            var item = PM.GetTable<Product>().Select(p=>new ProductItem
                {
                    ID=p.ID,
                    Name_Ch=p.ProductInfo_Ch.Name,
                    Name_En=p.ProductInfo_En.Name,
                    Dec_Ch=p.ProductInfo_Ch.Discription,
                    Dec_En=p.ProductInfo_En.Discription,
                    TypeID=p.TypeID,
                    Number=p.Identifier,
                    ClickCount=p.ClickCount,
                    DefaultPic = def != -1 ? (p.ProductPictures.Where(p1 => p1.IsDefault).Select(p1 => p1.FileName).FirstOrDefault() ?? p.ProductPictures.Where(p1 => p1.TypeID == def).Select(p1 => p1.FileName).FirstOrDefault() ?? p.ProductPictures.FirstOrDefault().FileName) : null,
                }).FirstOrDefault(p => p.ID == A);
            if (item == null)
                return RedirectToAction("index", new { L });

            PM.Update<Product>(new { ClickCount = new OpteratorValue(Opterator.Add, 1) }, new { item.ID });

            item.TypePath = CommenManage.ProductTypes.First(p => p.ID == item.TypeID).Parents;
            var tmp = CommenManage.ProductTypes.Where(p1 => p1.ID == item.TypeID || item.TypePath.Contains("," + p1.ID + ",")).OrderBy(p1 => p1.Parents.Length);

            var model = new DetailModel
            {
                Item = item,
                Disc_Ch = (item.Dec_Ch ?? "").Split(new string[] { "\n&\r" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p =>
                    {
                        var t = p.Split(new string[] { "\n:\r" }, StringSplitOptions.None);
                        return new NameValueItem<string, string> { Key = t[0], Value = t[1] };
                    }).ToArray(),
                Disc_En = (item.Dec_En ?? "").Split(new string[] { "\n&\r" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p =>
                    {
                        var t = p.Split(new string[] { "\n:\r" }, StringSplitOptions.None);
                        return new NameValueItem<string, string> { Key = t[0], Value = t[1] };
                    }).ToArray(),
                PictureTypes = CommenManage.ProductPictureTypes,
                TypeName_Ch = string.Join("->", tmp.Select(p1 => p1.Name_Ch).ToArray()),
                TypeName_En = string.Join("->", tmp.Select(p1 => p1.Name_En).ToArray()),
                Pictures = PM.GetTable<ProductPicture>().Where(p => p.ProductID == item.ID).Select(p => new PictureItem { ID = p.ID, TypeID = p.TypeID, FileName = p.FileName }).ToArray(),
            };

            return View("Detail_" + L, model);
        }
    }
}
